#include "main_begin.h"
#include "ui_main_begin.h"
#include "two_player.h"
#include <QPushButton>
#include <QCloseEvent>
#include <QAbstractButton>
#include <iostream>
#define Print(v) std::cout << #v << " : " << v << std::endl
#include "QWidget"
//自定义信号槽
void main_begin::Clicked_single(){
    //Print("btnsin");
    if(single == nullptr){
        single = new Widget;
        //connect信号槽
        //当单人模式窗口选择关闭时，就会重新打开主菜单
        connect(single, SIGNAL(close()), this, SLOT(show()));
    }else if(single->isOver()){//当游戏结束时，删除当前窗口，创建新的窗口对象
        delete single;
        single = new Widget;
        connect(single, SIGNAL(close()), this, SLOT(show()));
    }
    single->show();
    this->hide();
}

void main_begin::Clicked_dou(){
    Print("btnduo");
    if(double_ == nullptr){
        double_ = new Two_Player;
        connect(double_, SIGNAL(close_dou()), this, SLOT(show()));
    }
    else if(double_->isOver_dou()){
        delete double_;
        double_ = new Two_Player;
        connect(double_, SIGNAL(close_dou()), this, SLOT(show()));
    }
    double_->show();
    this->hide();
}
main_begin::main_begin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::main_begin)
{
    ui->setupUi(this);
    this->setWindowTitle("主菜单");
    //锁定窗口大小
    this->resize(600, 600);
    this->setMaximumSize(600, 600);
    this->setMinimumSize(600, 600);
    //创建需要的按钮
    QPushButton* single_=new QPushButton;
    QPushButton* double_=new QPushButton;
    QPushButton* exit=new QPushButton;
    single_->setParent(this);
    single_->setText("单人模式");
    single_->move(200, 100);
    single_->resize(200, 100);
    single_->show();
    double_->setParent(this);
    double_->setText("双人热座");
    double_->move(200,200);
    double_->resize(200, 100);
    double_->show();
    exit->setParent(this);
    exit->setText("退出游戏");
    exit->move(200, 300);
    exit->resize(200, 100);
    exit->show();
    //绑定三个信号槽
    connect(single_, SIGNAL(clicked()),this, SLOT(Clicked_single()));
    connect(double_, SIGNAL(clicked()), this, SLOT(Clicked_dou()));
    connect(exit,SIGNAL(clicked()),this,SLOT(close()));
}

main_begin::~main_begin()
{
    delete ui;
}
//关闭当前窗口函数
void main_begin::closeEvent(QCloseEvent *event)
{

    Print("true");
    event->accept();
}


